<?php
/**
 * fldcms
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2017-08-09
 */
namespace app\admin\controller;
use think\Request;
use think\helper\Time;
/**
 * 文章管理
 * @author fld
 *
 */
class ArticleController extends BaseController{
    function __construct(){
        parent::__construct();
        $this->db = model('Article');
    }
	/**
	 * 文章列表
	 */
	public function indexOp(){
		$result = $this->getCondition();
		$list = $this->db->getArticleList($result);
		$this->assign("list",$list);
		// 获取分页显示
		$page = $list->render();
		$this->assign('page', $page);
		//文章分类列表
        $cate_list = model('Cate')->getList();
        $this->assign('cate_list',$cate_list);
		return $this->fetch();
	}
	
	public function getCondition(){
		$result = array();
		$condition = array();
		$query = array();
		//关键字
		$keyword = input('param.keyword/s');
		if(!empty($keyword)){
			$condition['article_title|article_author'] = array('like','%'.$keyword.'%');
			$query['keyword'] = $keyword;
		}
		$this->assign('keyword',$keyword);
		//文章分类
		$class_id = input('param.class_id/d');
		if(!empty($class_id)){
			$condition['class_id'] = $class_id;
			$query['class_id'] = $class_id;
		}
		$this->assign('class_id',$class_id);
		$condition['is_delete'] = 0 ;
		$result['condition'] = $condition;
		$result['query'] = $query;
		return $result;
	}
	/**
	 * 添加文章信息
	 */
	public function addOp(){
	    if (chksubmit()){
	       $data = input('post.');
	       if(!empty(input('post.uploadfile'))){
				$logic_upload = model('Upload', 'Logic');
				$img_result = $logic_upload->upload('article_img','article_img');
				if($img_result['status'] == '1'){
					$data['article_img'] = $img_result['file_url'];
				}else{
				    $result['error'] = 1;
				    $result['msg'] = $img_result['msg'];
				    return json($result);
				}
			}
            $res = $this->db->addData($data);
            if($res !== false){
                $this->msg = "添加文章成功";
                $result['error'] = 0;
            }else{
                $this->msg = "添加文章失败";
                $result['error'] = 1;
            }
            $result['msg'] = $this->msg;
            return json($result);
	    }else{
	        //文章分类列表
	        $cate_list = model('Cate')->getList();
	        $this->assign('cate_list',$cate_list);
	        //标签信息
	        $tag_list = model('Tag')->getList();
	        $this->assign('tag_list',$tag_list);
	        return $this->fetch();
	    }
	}
	/**
	 * 编辑文章信息
	 */
	public function editOp(){
		if (chksubmit()){
		   $data = input('post.');
	       if(!empty(input('post.uploadfile'))){
				$logic_upload = model('Upload', 'Logic');
				$img_result = $logic_upload->upload('article_img','article_img');
				if($img_result['status'] == '1'){
					$data['article_img'] = $img_result['file_url'];
				}else{
				    $result['error'] = 1;
				    $result['msg'] = $img_result['msg'];
				    return json($result);
				}
			}
            $res = $this->db->updateData($data);
            if($res !== false){
                $this->msg = "编辑文章成功";
                $result['error'] = 0;
            }else{
                $this->msg = "编辑文章失败";
                $result['error'] = 1;
            }
            $result['msg'] = $this->msg;
            return json($result);
		}else{
			//文章信息
			$id = input('param.id');
			$article_info = $this->db->getArticleInfo(array('id'=>$id));
			$this->assign('vo',$article_info);
			//文章分类列表
			$cate_list = model('Cate')->getList();
			$this->assign('cate_list',$cate_list);
			//获取文章标签信息
			$article_tag_list = model('ArticleTag')->getArticleTagList(array('article_id'=>$id));
			$this->assign('article_tag_list',$article_tag_list);
			//标签信息
			$tag_list = model('Tag')->getRemoveList($article_tag_list);
			$this->assign('tag_list',$tag_list);
			return $this->fetch(); 
		}
	}
	/**
	 * 删除文章信息
	 */
	public function deleteOp(){
		$result = array();
		$id = input('param.id');
		$res = $this->db->deleteData(array('id'=>$id));
		if($res !== false){
		    $result['error'] = 0;
		}else{
			$this->msg = "删除文章失败";
			$result['error'] = 1;
		}
		$result['msg'] = $this->msg;
		return json($result);
	}
	
}